/*   1:    */package com.mrd.dolphin.net.mina.codec;
/*   2:    */
/*   3:    */

import com.mrd.packet.PbPacket;

/*   4:    */import java.io.PrintStream;
/*   5:    */import org.apache.mina.core.buffer.IoBuffer;
/*   6:    */import org.apache.mina.core.session.AttributeKey;
/*   7:    */import org.apache.mina.core.session.IoSession;
/*   8:    */import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
/*   9:    */import org.apache.mina.filter.codec.ProtocolDecoderOutput;
/*  10:    */import org.slf4j.Logger;
/*  11:    */import org.slf4j.LoggerFactory;
/*  12:    */
/*  26:    */public class PbDecoder
/*  27:    */  extends ProtocolDecoderAdapter
/*  28:    */{
/*  29: 29 */  private static final Logger log = LoggerFactory.getLogger(PbDecoder.class);
/*  30:    */  
/*  31: 31 */  private final AttributeKey IOBUFFER = new AttributeKey(getClass(), "IOBUFFER");
/*  32:    */  
/*  33:    */  public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
/*  34:    */  {
/*  35: 35 */    IoBuffer buffer = (IoBuffer)session.getAttribute(this.IOBUFFER);

/*  37: 37 */    boolean usingSessionBuffer = true;
/*  38: 38 */    if (buffer != null) {
/*  39: 39 */      buffer.put(in);
/*  40: 40 */      buffer.flip();
/*  41:    */    } else {
/*  42: 42 */      buffer = in;
/*  43: 43 */      usingSessionBuffer = false;
/*  44:    */    }
/*  45:    */    
/*  46: 46 */    while (buffer.hasRemaining()) {
/*  47: 47 */      boolean b = doDecode(session, buffer, out);
/*  48: 48 */      if (!b)
/*  49:    */        break;
/*  50:    */    }
/*  51: 51 */    if (buffer.hasRemaining()) {
/*  52: 52 */      if (usingSessionBuffer) {
/*  53: 53 */        buffer.compact();
/*  54:    */      } else {
/*  55: 55 */        storeRemainingInSession(buffer, session);
/*  56:    */      }
/*  57:    */    }
/*  58: 58 */    else if (usingSessionBuffer) {
/*  59: 59 */      removeSessionBuffer(session);
/*  60:    */    }
/*  61:    */  }
/*  62:    */  
/*  63:    */  protected boolean doDecode(IoSession session, IoBuffer buffer, ProtocolDecoderOutput out) throws Exception
/*  64:    */  {
/*  65: 65 */    if (buffer.remaining() < 24) {
/*  66: 66 */      
/*  67: 67 */      return false;
/*  68:    */    }
/*  69: 69 */    int dataLen = buffer.getInt();
/*  70: 70 */    if (dataLen == 0) {
/*  71: 71 */      
/*  72: 72 */      int packetId = buffer.getInt();
/*  73: 73 */      long stamp = buffer.getLong();
/*  74: 74 */      int retCode = buffer.getInt();
/*  75: 75 */      int flag = buffer.getInt();

/*  78: 78 */      if (packetId == 1) {

/*  80:    */      }
/*  81: 81 */      PbPacket pb = new PbPacket();
/*  82: 82 */      pb.setPacketId(packetId);
/*  83: 83 */      pb.setStamp(stamp);
/*  84: 84 */      pb.setRetCode(retCode);
/*  85: 85 */      pb.setFlag(flag);
/*  86: 86 */      out.write(pb);
/*  87: 87 */      return true;
/*  88:    */    }
/*  89:    */    
/*  91: 91 */    if (buffer.remaining() >= dataLen + 20) {
/*  92:    */      try {
/*  93: 93 */        if (dataLen < 0) {

/*  95: 95 */          System.err.println(buffer.remaining() + ", " + dataLen);
/*  96: 96 */          return false;
/*  97:    */        }
/*  98: 98 */        int packetId = buffer.getInt();
/*  99: 99 */        long stamp = buffer.getLong();
/* 100:100 */        int retCode = buffer.getInt();
/* 101:101 */        int flag = buffer.getInt();

/* 1
/* 107:    */        
/* 108:108 */        byte[] body = new byte[dataLen];
				
/* 109:109 */        buffer.get(body);
/* 110:    */        
/* 111:111 */        PbPacket pb = new PbPacket(body);
/* 112:112 */        pb.setPacketId(packetId);
/* 113:113 */        pb.setStamp(stamp);
/* 114:114 */        pb.setRetCode(retCode);
/* 115:115 */        pb.setFlag(flag);

/* 117:117 */        out.write(pb);
/* 118:118 */        return true;
/* 119:    */      } catch (Exception e) {
/* 120:120 */       
/* 121:121 */        buffer.position(buffer.limit());
/* 122:122 */        removeSessionBuffer(session);
/* 123:123 */        e.printStackTrace();
/* 124:124 */        return false;
/* 125:    */      }
/* 126:    */    }
/* 127:127 */    buffer.position(buffer.position() - 4);

/* 130:130 */    return false;
/* 131:    */  }
/* 132:    */  
/* 133:133 */  protected void removeSessionBuffer(IoSession session) { session.removeAttribute(this.IOBUFFER); }
/* 134:    */  
/* 135:    */  public void finishDecode(IoSession session, ProtocolDecoderOutput out)
/* 136:    */    throws Exception
/* 137:    */  {}
/* 138:    */  
/* 139:    */  private void storeRemainingInSession(IoBuffer buf, IoSession session)
/* 140:    */  {
/* 141:141 */    IoBuffer remainingBuf = IoBuffer.allocate(buf.capacity()).setAutoExpand(true);
/* 142:142 */    remainingBuf.order(buf.order());
/* 143:143 */    remainingBuf.put(buf);
/* 144:    */    
/* 145:145 */    session.setAttribute(this.IOBUFFER, remainingBuf);
/* 146:    */  }
/* 147:    */  
/* 148:    */  public void dispose(IoSession session)
/* 149:    */    throws Exception
/* 150:    */  {}
/* 151:    */}


/* Location:           D:\MMOServer\MMOServer\lib\mrd-framework\
 * Qualified Name:     com.mrd.dolphin.net.mina.codec.PbDecoder
 * JD-Core Version:    0.7.0-SNAPSHOT-20130630
 */